<template>
  <div class="app-container home">
    <el-divider />
    <el-row :gutter="20">
      <el-col :xs="24" :sm="24" :md="12" :lg="12">
        <el-card shadow="always" style="padding-bottom: 20px">
          <div>
            <el-row :gutter="40" class="panel-group">
              <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
                <div class="card-panel" @click="handleGo('/warehouse')">
                  <div class="card-panel-icon-wrapper icon-message">
                    <svg-icon icon-class="table" class-name="card-panel-icon" />
                  </div>
                  <div class="card-panel-description">
                    <div class="card-panel-text">仓库</div>
                    <span style="font-size:20px">{{ warehouseList.length }}</span>
                  </div>
                </div>
              </el-col>
              <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
                <div class="card-panel" @click="handleGo('/customer/supplier')">
                  <div class="card-panel-icon-wrapper icon-people">
                    <svg-icon icon-class="peoples" class-name="card-panel-icon" />
                  </div>
                  <div class="card-panel-description">
                    <div class="card-panel-text">供应商</div>
                      <span style="font-size:20px">{{ supplierList.length }}</span>
                  </div>
                </div>
              </el-col>
              <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
                <div class="card-panel" @click="handleGo('/customer/carrier')">
                  <div class="card-panel-icon-wrapper icon-people">
                    <svg-icon icon-class="guide" class-name="card-panel-icon" />
                  </div>
                  <div class="card-panel-description">
                    <div class="card-panel-text">承运商</div>
                      <span style="font-size:20px">{{ carrierList.length }}</span>
                  </div>
                </div>
              </el-col>
              <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
                <div class="card-panel" @click="handleGo('/customer/customer')">
                  <div class="card-panel-icon-wrapper icon-message">
                    <svg-icon icon-class="star" class-name="card-panel-icon" />
                  </div>
                  <div class="card-panel-description">
                    <div class="card-panel-text">客户</div>
                      <span style="font-size:20px">{{ customerList.length }}</span>
                  </div>
                </div>
              </el-col>
              <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
                <div class="card-panel" @click="goAnchor('warn')">
                  <div class="card-panel-icon-wrapper icon-money">
                    <svg-icon icon-class="eye-open" class-name="card-panel-icon" />
                  </div>
                  <div class="card-panel-description">
                    <div class="card-panel-text">预警</div>
                      <span style="font-size:20px">{{ yujingList.length }}</span>
                  </div>
                </div>
              </el-col>
              <el-col :xs="12" :sm="12" :lg="8" class="card-panel-col">
                <div class="card-panel" @click="goAnchor('expiry')">
                  <div class="card-panel-icon-wrapper icon-money">
                    <svg-icon icon-class="bug" class-name="card-panel-icon" />
                  </div>
                  <div class="card-panel-description">
                    <div class="card-panel-text">过期</div>
                      <span style="font-size:20px">{{ itemList.filter((item) => item.isExpired).length }}</span>
                  </div>
                </div>
              </el-col>
            </el-row>
          </div>
        </el-card>
        
        <el-card class="update-log" style="margin: 15px -5px" id="warn">
          <template v-slot:header>
            <div class="clearfix">
              <span>预警</span>
            </div>
          </template>
          <el-table :data="yujingList" border="1">
            <el-table-column label="物料编号" align="center" prop="item_id" />
            <el-table-column label="名称" align="center" prop="item_name" />
            <el-table-column label="所属仓库" align="center" prop="warehouse_name" />
            <el-table-column label="当前库存" align="center" prop="quan" />
            <el-table-column label="安全库存" align="center" prop="quantity" />
          </el-table>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="12">
        <el-card class="update-log" style="margin: -5px -5px" id="expiry">
          <template v-slot:header>
            <div class="clearfix">
              <span>过期物料</span>
            </div>
          </template>
          <el-table :data="itemList.filter((item) => item.isExpired)" border="1">
            <el-table-column label="编号" align="center" prop="itemNo" />
            <el-table-column label="名称" align="center" prop="itemName" />
            <el-table-column label="分类" align="center" prop="typeName" />
            <el-table-column label="单位类别" align="center" prop="unit" />
            <el-table-column label="安全库存" align="center" prop="quantity" />
            <el-table-column label="有效期" align="center" prop="expiryDate" width="180">
              <template #default="scope">
                <span :style="{ color: 'red' }">{{
                  parseTime(scope.row.expiryDate, "{y}-{m}-{d} {h}:{i}:{s}")
                }}</span>
                <span v-if="scope.row.isExpired" :style="{ color: 'red' }"
                  ></span
                >
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup name="Index">
import { ref, onMounted } from "vue";
import { ElTable, ElTableColumn, ElPagination } from "element-plus";
import { selectItem, select ,editDelFlag} from "@/api/wms/item";
import {listWarehouse} from "@/api/wms/warehouse";
import { listSupplier} from "@/api/wms/supplier";
import { listCarrier } from "@/api/wms/carrier";
import { listCustomer } from "@/api/wms/customer";
const { proxy } = getCurrentInstance();

const version = ref("3.8.4");
const warehouseList = ref([]);
const supplierList = ref([]);
const carrierList = ref([]);
const customerList = ref([]);
const yujingList = ref([]);
const itemList = ref([]);
function goTarget(url) {
  window.open(url, "__blank");
}
const total = ref(0);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    itemNo: null,
    itemName: null,
    specification: null,
    itemType: null,
    unit: null,
    unitPrice: null,
    areaId: null,
    warehouseId: null,
    quantity: null,
    productionDate: null,
    expiryDate: null,
    batch: null,
  },
});
const { queryParams } = toRefs(data);
// 判断有效期是否超过当前时间的函数
const isExpired = (dateStr) => {
  const expiryDate = new Date(dateStr);
  const now = new Date();
  return expiryDate < now;
};

/** 查询物料列表 */
function getList() {
  selectItem(queryParams.value).then((response) => {
    console.log(response.rows)
    itemList.value = response.rows;
    total.value = response.total;
    // 遍历获取到的物料列表，为每个物料添加是否过期的标识
    itemList.value.forEach((item) => {
      item.isExpired = isExpired(item.expiryDate);
    });
  });
}


//跳转路径
function handleGo(path){
proxy.$router.push(path)
}
/** 查询预警 */
function getYuJingList() {
  select().then((resp) => {
    // console.log(resp.rows);
    yujingList.value = resp.rows;
    // console.log(yujingList);
  });
}

/** 查询仓库列表 */
function getListcq() {
  listWarehouse().then(response => {
    warehouseList.value = response.rows;
    // console.log(warehouseList.value);
  });
}
/** 查询供应商列表 */
function getListgy() {
  listSupplier().then(response => {
    supplierList.value = response.rows; 
  });
}
/** 查询承运商列表 */
function getListcy() {
  listCarrier().then(response => {
    carrierList.value = response.rows;
  });
}
/** 查询客户列表 */
function getListkh() {
  listCustomer().then(response => {
    customerList.value = response.rows;
  });
}


//点击图标滑动到指定位置
function goAnchor(selector) {
  selector = !"warn" ? "expiry" : "warn";
  document.getElementById(selector).scrollIntoView({
    behavior: "smooth",
    block: "start",
  });
}
onMounted(() => {
  getList();
  getYuJingList();
  getListcq();
  getListgy();
  getListcy();
  getListkh();
});
</script>

<style scoped>
.home-wrapper

  .li {
    float: left;
    width: 25%;
    font-size: 12px;
    color: rgb(185, 181, 189);
  }

  .da {
    float: left;
    width: 25%;
    font-size: 20px;
  }

  .first {
    display: flex;
    margin-bottom: 20px;
    align-items: center;
  }

  .el-table .warning-row {
    background: rgb(39, 138, 230);
  }
  .panel-group {
    margin-top: 18px;

    .card-panel-col {
      margin-bottom: 32px;
    }

    .card-panel {
      height: 108px;
      cursor: pointer;
      font-size: 12px;
      position: relative;
      overflow: hidden;
      color: #666;
      background: #fff;
      box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
      border-color: rgba(0, 0, 0, .05);

      &:hover {
        .card-panel-icon-wrapper {
          color: #fff;
        }

        .icon-people {
          background: #40c9c6;
        }

        .icon-message {
          background: #36a3f7;
        }

        .icon-money {
          background: #f4516c;
        }
      }

      .icon-people {
        color: #40c9c6;
      }

      .icon-message {
        color: #36a3f7;
      }

      .icon-money {
        color: #f4516c;
      }

      .card-panel-icon-wrapper {
        float: left;
        margin: 14px 0 0 14px;
        padding: 16px;
        transition: all 0.38s ease-out;
        border-radius: 6px;
      }

      .card-panel-icon {
        float: left;
        font-size: 48px;
      }

      .card-panel-description {
        float: right;
        font-weight: bold;
        margin: 26px;
        margin-left: 0px;

        .card-panel-text {
          line-height: 18px;
          color: rgba(0, 0, 0, 0.45);
          font-size: 16px;
          margin-bottom: 12px;
        }

        .card-panel-num {
          font-size: 20px;
        }
      }
    }
  }

  @media (max-width:550px) {
    .card-panel-description {
      display: none;
    }

    .card-panel-icon-wrapper {
      float: none !important;
      width: 100%;
      height: 100%;
      margin: 0 !important;

      .svg-icon {
        display: block;
        margin: 14px auto !important;
        float: none !important;
      }
    }
  }
</style>

